#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
   @Author: 即将拥有人鱼线的yjw
   @Time: 2024/12/8 21:14
   @File: generatedData.py
   @desc:
"""
from copy import copy
from DataTest.tabledata.commonData.agencyClient import get_agency_client
from DataTest.tabledata.commonData.autoParsing import get_user_info
from DataTest.tabledata.commonData.client_id import get_client_id, generate_fund_and_trade_accounts, generate_trade, \
    generate_account_request
from DataTest.tabledata.commonData.fndAccountRequest import get_account_request_data
from DataTest.tabledata.commonData.fndCertificate import get_certificate_data
from DataTest.tabledata.commonData.fndClient import get_client_data
from DataTest.tabledata.commonData.fndConfirm import get_confirm_data
from DataTest.tabledata.commonData.fndFundAccount import get_fund_account_data
from DataTest.tabledata.commonData.fndFundInfo import get_request_data
from DataTest.tabledata.commonData.personClient import get_person_client
from DataTest.tabledata.commonData.productClient import get_product_client
from DataTest.tabledata.dataTest import montage_sql



def generated_data(text,env=None):
     clients = []
     trades = []
     account_trade = []
     dict_entries = get_user_info(text)
     client_id  = get_client_id(env)
     client_list = {'client_id':client_id}
     if dict_entries['个人客户']:
         keywords = dict_entries['个人客户']

         if isinstance(keywords, (dict, list)):
             keywords.update({'client_type': '0'})
             client_list.update(get_person_client())
             client_list.update(generate_fund_and_trade_accounts('个人',env))
             client_list.update(get_client_data(keywords))
             client_list.update(get_fund_account_data(keywords))
             client_list.update(get_certificate_data(keywords))
             clients.append(client_list)
             insert_client([client_list], env)
         else:
             for i in range(int(keywords)):
                 client_list ={}
                 client_list.update({'client_id': get_client_id(env)})
                 client_list.update(get_person_client())
                 client_list.update(generate_fund_and_trade_accounts('个人',env))
                 insert_client([client_list], env)
                 clients.append(client_list)

     if dict_entries['机构客户']:
         keywords = dict_entries['机构客户']
         if isinstance(keywords, (dict, list)):
                 keywords.update({'client_type': '1'})
                 client_list.update(get_agency_client())
                 client_list.update(generate_fund_and_trade_accounts('机构',env))
                 client_list.update(get_client_data(keywords))
                 client_list.update(get_fund_account_data(keywords))
                 client_list.update(get_certificate_data(keywords))
                 clients.append(client_list)
                 insert_client([client_list], env)


         else:
             for i in range(int(keywords)):
                 client_list ={}
                 client_list.update({'client_id': get_client_id(env)})
                 client_list.update(get_agency_client())
                 client_list.update(generate_fund_and_trade_accounts('机构',env))
                 clients.append(client_list)
                 insert_client([client_list], env)


     if dict_entries['产品客户']:
         keywords = dict_entries['产品客户']
         if isinstance(keywords, (dict, list)):
                 keywords.update({'client_type': '2'})
                 client_list.update(get_product_client())
                 client_list.update(generate_fund_and_trade_accounts('产品',env))
                 client_list.update(get_client_data(keywords))
                 client_list.update(get_fund_account_data(keywords))
                 client_list.update(get_certificate_data(keywords))
                 clients.append(client_list)
                 insert_client([client_list], env)

         else:
             for i in range(int(keywords)):
                 client_list = {}
                 client_list.update({'client_id': get_client_id(env)})
                 client_list.update(get_product_client())
                 client_list.update(generate_fund_and_trade_accounts('产品',env))
                 clients.append(client_list)
                 insert_client([client_list], env)

     client = clients[0]
     print(f"insert_client{clients}")
     client_copy = copy(client)
     if dict_entries['基金交易']:
         keywords = dict_entries['基金交易']
         if isinstance(keywords, (dict, list)):
             client_copy.update(generate_trade())
             client_copy.update(get_request_data(keywords))
             client_copy.update(get_confirm_data(keywords))
             insert_trade(trades, env)
         else:
             for i in range(int(keywords)):
                client_list = client_copy
                client_trade_list = generate_trade()
                client_list.update(client_trade_list)
                insert_trade(trades, env)


     if dict_entries['账户流水']:
         keywords = dict_entries['账户流水']
         client.update(generate_account_request())
         client.update(get_account_request_data(keywords))
         account_trade.append(client)
     insert_account_trade(account_trade,env)
     return_list = []
     for item in clients:
         return_list.append({'client_id': item['client_id'],'client_type': item['client_type'],'fund_account': item['fund_account']})
     print(return_list)
     return return_list


def insert_client(sqls,env):
    for sql_list in sqls:
        montage_sql(table='fnd_client', sql_list=sql_list, env=env)
        montage_sql(table='fnd_fund_account', sql_list=sql_list, env=env)
        montage_sql(table='fnd_certificate', sql_list=sql_list, env= env)

def insert_trade(sqls,env):
    if sqls:
        for sql_list in sqls:
            montage_sql(table='fnd_request', sql_list=sql_list, env=env)
            montage_sql(table='fnd_confirm', sql_list=sql_list, env=env)

def insert_account_trade(sqls,env):
    if sqls:
        for sql_list in sqls:
            montage_sql(table='fnd_account_request', sql_list=sql_list, env=env)

if __name__=='__main__':

    # keyword = f"个人客户：国家代码为中国；基金交易：成交金额为500万，业务标志为认购"
    # keyword = f'个人客户：2'
    # keyword = f'个人客户：2；机构客户：2；产品客户：2'
    # keyword = '个人客户：1；基金交易：申请日期为20240320，业务标志为申购，申请金额为20w，成交金额为20w，确认份额为20w，净值为1.156；'
    # keyword = f'个人客户：1； 基金交易：6；账户流水：业务标志为账户修改;'
    # keyword = f'产品客户：1'
    keyword = f"个人客户：客户姓名为刘雨鑫，证件号码为612301199110100416，性别代码为男，出生日期为19911010，国家代码为中国；"

    generated_data(keyword, 'BigData')


